//
//  RecordTableViewController.m
//  bitcoin
//
//  Created by caiyajun on 2018/5/28.
//  Copyright © 2018年 比特币. All rights reserved.
//

#import "RecordTableViewController.h"
#import "RecordBottomView.h"
#import "RecordCell.h"

#import "RinseModel.h"
#import "DrawModel.h"

#import "MJRefresh.h"
#import "XTNetReloader.h"
#import "WeChatStylePlaceHolder.h"

#import "RinseMoneyController.h"

#import "DrawMoneyViewController.h"
@interface RecordTableViewController ()
@property (nonatomic, strong) RecordBottomView *recordBottomView;
@property (weak, nonatomic) IBOutlet UIButton *rinseBtn;
@property (weak, nonatomic) IBOutlet UIButton *drawBtn;
@property (weak, nonatomic) IBOutlet UILabel *markLabel;
@property (weak, nonatomic) IBOutlet UILabel *amountLabel;
@property (weak, nonatomic) IBOutlet UILabel *forzenLabel;
@property (weak, nonatomic) IBOutlet UILabel *assessmentLabel;
@property (weak, nonatomic) IBOutlet UILabel *moneyKindLabel;


@property (nonatomic, copy) NSString *moneyKind;

@property (nonatomic, strong) MJRefreshAutoNormalFooter *footer;
@property (nonatomic, strong) MJRefreshAutoNormalFooter *nilFooter;

@property (nonatomic, strong) NSMutableArray *dataSource;

@property (nonatomic, assign) NSInteger pageNumber;
@property (nonatomic, assign) NSInteger pageSize;
@end

@implementation RecordTableViewController
- (NSMutableArray *)dataSource {
    if (_dataSource == nil) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.pageSize=1;
    
    [self.rinseBtn setBackgroundImage:[UIImage imageWithColor:RGBColor(123, 78, 225)] forState:UIControlStateSelected];
    [self.rinseBtn setBackgroundImage:[UIImage imageWithColor:RGBColor(37, 41, 61)] forState:UIControlStateNormal];
    self.rinseBtn.selected=YES;
    
    [self.drawBtn setBackgroundImage:[UIImage imageWithColor:RGBColor(123, 78, 225)] forState:UIControlStateSelected];
    [self.drawBtn setBackgroundImage:[UIImage imageWithColor:RGBColor(37, 41, 61)] forState:UIControlStateNormal];
    self.drawBtn.selected=NO;
    
    
    
    if ([[UserDefaults objectForKey:K_PRICE] isEqualToString:@"2"]){
        self.moneyKind=@"USD";
    }else if ([[UserDefaults objectForKey:K_PRICE] isEqualToString:@"3"]){
        self.moneyKind=@"AUD";
    }else{
        self.moneyKind=@"CNY";
    }
    self.amountLabel.text=_accountModel.amount;
    self.forzenLabel.text=_accountModel.forzen_amount;
    self.assessmentLabel.text=_accountModel.assessment;
    self.markLabel.text=_accountModel.mark;
    self.moneyKindLabel.text=[NSString stringWithFormat:@"估算(%@)",self.moneyKind];
    
    UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"arrowleft-icon"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
    self.navigationItem.leftBarButtonItem = back;
    
//    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(beginToRequestData)];
//    //    [footer setTitle:@"点击或下拉加载更多" forState:MJRefreshStateIdle];
//    [footer setTitle:@"加载中" forState:MJRefreshStateRefreshing];
//    footer.stateLabel.textColor = RGBColor(153, 153, 153);
//    self.footer = footer;
    [self requestData];
    
}

- (void)backAction:(UIBarButtonItem *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)beginToRequestData{
    self.pageSize++;
    [self requestData];
}



- (IBAction)backAction {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
//    [self.navigationController setNavigationBarHidden:YES animated:NO];
//    [self setStatusBarBackgroundColor:RGBColor(46, 49, 69)];
    
    
    RecordBottomView *recordBottomView = [RecordBottomView xib];
    self.recordBottomView=recordBottomView;
    [[UIApplication sharedApplication].keyWindow addSubview:recordBottomView];
    //    __weak typeof(recordBottomView) weakRecordBottomView = recordBottomView;
    recordBottomView.submitActionBlock = ^(NSInteger tag) {
        if (tag==0) {
            NSLog(@"充币");
            [self performSegueWithIdentifier:@"rinseMoneySegue" sender:nil];
        }else if (tag==1){
            NSLog(@"提币");
            [self performSegueWithIdentifier:@"drawMoneySegue" sender:nil];
        }else if (tag==2){
            NSLog(@"交易");
        }
        
        
        
        
        
        
    };
    
}
- (void)setStatusBarBackgroundColor:(UIColor *)color {
    
    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
        statusBar.backgroundColor = color;
    }
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
//    [self.navigationController setNavigationBarHidden:NO animated:NO];
//    [self setStatusBarBackgroundColor:[UIColor clearColor]];
    [self.recordBottomView removeFromSuperview];
}

- (void)requestData{
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(beginToRequestData)];
    //    [footer setTitle:@"点击或下拉加载更多" forState:MJRefreshStateIdle];
    [footer setTitle:@"加载中" forState:MJRefreshStateRefreshing];
    footer.stateLabel.textColor = RGBColor(153, 153, 153);
    self.footer = footer;
    
    
    self.pageNumber=15;
    NSDictionary *param = @{
                            @"currency_id" : self.accountModel.currency_id,
                            @"list_rows" : @(self.pageNumber),
                            @"page" : @(self.pageSize)};
    if (self.rinseBtn.selected) {
        
        [LSRequestManager getRequestWithURL:HUOBI(@"api/deposit/depositList")
                                     params:param
                                    success:^(NSDictionary *responseData) {
                                        if ([responseData[@"ret"] integerValue ] == 0) {
                                            if (self.pageSize==1) {
                                                [self.dataSource removeAllObjects];
                                            }
                                            NSArray *array = [RinseModel mj_objectArrayWithKeyValuesArray:responseData[@"data"][@"data"]];
                                            [self.dataSource addObjectsFromArray:array];
                                            if (array.count< self.pageNumber) {
                                                self.tableView.mj_footer = self.nilFooter;
                                            } else {
                                                self.tableView.mj_footer = self.footer;
                                                //                                            self.footer.refreshingTitleHidden = YES;
                                            }
                                            [self.tableView reloadData];
                                        } else {
                                            
                                            [MBProgressHUD showError:responseData[@"msg"]];
                                        }
                                    }
                                    failure:^(NSError *error) {
                                        
                                    }
                                    showHUD:NO];
    }else{
        
        [LSRequestManager getRequestWithURL:HUOBI(@"api/withdraw/withdrawList")
                                     params:param
                                    success:^(NSDictionary *responseData) {
                                        if ([responseData[@"ret"] integerValue ] == 0) {
                                            
                                            
                                            
                                            //如果是下拉刷新, 先清空所有的数据
                                            if (self.pageSize==1) {
                                                [self.dataSource removeAllObjects];
                                            }
                                            NSArray *array = [DrawModel mj_objectArrayWithKeyValuesArray:responseData[@"data"][@"data"]];
                                            [self.dataSource addObjectsFromArray:array];
                                            if (array.count < self.pageNumber) {
                                                self.tableView.mj_footer = self.nilFooter;
                                            } else {
                                                self.tableView.mj_footer = self.footer;
                                                //                                            self.footer.refreshingTitleHidden = YES;
                                            }
                                            [self.tableView reloadData];
                                        } else {
                                            
                                            [MBProgressHUD showError:responseData[@"msg"]];
                                        }
                                    }
                                    failure:^(NSError *error) {
                                        
                                    }
                                    showHUD:NO];
    }
    
}




#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    RecordCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RecordCell" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[RecordCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RecordCell"];
    }
    if (self.rinseBtn.selected){
        RinseModel*rinseModel=self.dataSource[indexPath.row];
        cell.timeLabel.text=rinseModel.update_time;
        cell.countLabel.text=rinseModel.amount;
        cell.ststusLabel.text=rinseModel.status;
        if ([rinseModel.status isEqualToString:@"2"]||[rinseModel.status isEqualToString:@"5"]) {
            cell.ststusLabel.tintColor=RGBColor(255, 174, 29);
        }else if ([rinseModel.status isEqualToString:@"4"]){
            cell.ststusLabel.tintColor=RGBColor(204, 204, 204);
        }else{
           cell.ststusLabel.tintColor=RGBColor(123, 78, 225);
        }
    }else{
        DrawModel*drawModel=self.dataSource[indexPath.row];
        cell.timeLabel.text=drawModel.update_time;
        cell.countLabel.text=drawModel.amount;
        cell.ststusLabel.text=drawModel.status;
        if ([drawModel.status isEqualToString:@"3"]) {
            cell.ststusLabel.tintColor=RGBColor(255, 174, 29);
        }else if ([drawModel.status isEqualToString:@"2"]){
            cell.ststusLabel.tintColor=RGBColor(204, 204, 204);
        }else{
            cell.ststusLabel.tintColor=RGBColor(123, 78, 225);
        }
    }
    
    return cell;
    
}
- (IBAction)rinseAction {
    self.drawBtn.selected=NO;
    self.rinseBtn.selected=YES;
    self.pageSize=1;
    [self requestData];
    
}
- (IBAction)drawAction {
    self.rinseBtn.selected=NO;
    self.drawBtn.selected=YES;
    self.pageSize=1;
    [self requestData];
    
    
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([segue.identifier isEqualToString:@"rinseMoneySegue"]){
        RinseMoneyController *rinseMoneyCtrl = segue.destinationViewController;
        rinseMoneyCtrl.mark = self.accountModel.mark;
    }else if ([segue.identifier isEqualToString:@"drawMoneySegue"]){
        DrawMoneyViewController *drawMoneyCtrl = segue.destinationViewController;
        drawMoneyCtrl.mark = self.accountModel.mark;
        drawMoneyCtrl.money=self.amountLabel.text;
        drawMoneyCtrl.currency_id=self.accountModel.currency_id;
    }
    
}




@end
